import BluetoothModel, { BondState, DeviceState } from './bluetoothmodel';
import ConfigData from '../utils/configdata';
import BluetoothDevice from './bluetoothdevice'


const TAG: string = 'BtConnector[BtController]'

export default class BtController {

    pairedDevices: BluetoothDevice[] = []

    public get(): Array<BluetoothDevice> {return this.pairedDevices}

    public refreshPairedDevices() {
        let deviceIds: string[] = BluetoothModel.getPairedDeviceIds();
        //console.log(`${TAG}bluetooth paired devices. MAC list = ` + JSON.stringify(deviceIds))
        let list: BluetoothDevice[] = []
        deviceIds.forEach(deviceId => {
            list.push(this.getDevice(deviceId));
        });
        this.pairedDevices = list;
        //console.log(`${TAG}bluetooth paired devices. list = ` + JSON.stringify(list))
    }

    private getDevice(deviceId: string): BluetoothDevice {
        let device = new BluetoothDevice();
        device.deviceId = deviceId;
        device.deviceName = BluetoothModel.getDeviceName(deviceId);
        device.deviceType = BluetoothModel.getDeviceType(deviceId);
        device.setProfiles(BluetoothModel.getDeviceState(deviceId));
        return device;
    }

    setEnable(en: boolean) {
        if (en) {
            BluetoothModel.enableBluetooth();
        } else {
            BluetoothModel.disableBluetooth()
        }
    }

    isStateOn(): boolean {return BluetoothModel.isStateOn()}

    connect(deviceId: string): Array<{
        profileId: number;
        connectRet: boolean;
    }> {
        return BluetoothModel.connectDevice(deviceId);
    }

    disconnect(deviceId: string): Array<{
        profileId: number;
        disconnectRet: boolean;
    }> {
        return BluetoothModel.disconnectDevice(deviceId);
    }
}